package com.css.dzj.dzjwgr.modules.appeal.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjwgr.common.audit.service.SysAuditService;
import com.css.dzj.dzjwgr.common.util.BeanUtil;
import com.css.dzj.dzjwgr.common.util.Constant;
import com.css.dzj.dzjwgr.common.util.R;
import com.css.dzj.dzjwgr.modules.appeal.entity.JgWgrTsjbglEntity;
import com.css.dzj.dzjwgr.modules.appeal.service.appealService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 投诉举报管理Controller
 */
@PropertySource(value = "classpath:base.properties")
@RestController
@RequestMapping(value = "/appeal")
public class appealController {
    @Autowired
    private appealService appealService;
    // 门户系统url
    @Value("${MAIN_WORK_URL_FW}")
    private String mainUrl;
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 条件查询
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam(name = "pageSize", required = false) Integer pageSize,
                     @RequestParam(name = "pageNo", required = false) Integer pageNo,
                     @RequestParam(name = "name", required = false) String name,
                     @RequestParam(name = "appealStatus", required = false) String appealStatus,
                     @RequestParam(name = "records", required = false) String records,
                     @RequestParam(name = "startTime", required = false) String startTime,
                     @RequestParam(name = "endTime", required = false) String endTime) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults<JgWgrTsjbglEntity> page = appealService.getPage(pageRequest, name, appealStatus, records,
                startTime, endTime);
        return R.ok().put("data", page);
    }

    /**
     * 查看详情回复页面
     *
     * @return
     */
    @GetMapping(value = "/getOne")
    public R getOne(String id) {
        JgWgrTsjbglEntity appeal = appealService.queryOne(id);
        return R.ok().put("data", appeal);
    }

    /**
     * 新增投诉举报信息
     */
    @PostMapping(value = "/addAppeal")
    public R addAppeal(@RequestBody JgWgrTsjbglEntity jgWgrTsjbglEntity) {
        String msg = appealService.addReply(jgWgrTsjbglEntity);
        return R.ok().put("data", msg);
    }

    /**
     * 修改投诉举报
     *
     * @param jgWgrTsjbglEntity
     * @return
     */
    @PostMapping(value = "/update")
    public R update(@RequestBody JgWgrTsjbglEntity jgWgrTsjbglEntity) {
        String s = "";
        try {
            JgWgrTsjbglEntity old = appealService.queryOne(jgWgrTsjbglEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(jgWgrTsjbglEntity, old);
            appealService.update(old);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 导出选中数据
     *
     * @param response
     * @param ids
     * @return
     */
    @GetMapping("/exportByIds")
    public R exportAppealToExcel(HttpServletResponse response, @RequestParam String ids) {
        String s = "";
        try {
            List<JgWgrTsjbglEntity> jgWgrTsjbglEntities = appealService.queryByIds(ids);
            appealService.exportAppealToExcel(response, jgWgrTsjbglEntities);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 批量导出
     *
     * @param response
     * @param name
     * @param appealStatus
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam(name = "records", required = false) String records,
                    @RequestParam(name = "name", required = false) String name,
                    @RequestParam(name = "startTime", required = false) String startTime,
                    @RequestParam(name = "endTime", required = false) String endTime,
                    @RequestParam(name = "appealStatus", required = false) String appealStatus) {
        try {
            List<JgWgrTsjbglEntity> jgWgrTsjbglEntities = appealService.queryByParams(records,name,
                    startTime, endTime,appealStatus);
            appealService.exportAppealToExcel(response, jgWgrTsjbglEntities);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 查看详情回复页面(对门户)
     *
     * @return
     */
    @PostMapping(value = "/getAppeal")
    public JgWgrTsjbglEntity getAppeal(String id) {
        JgWgrTsjbglEntity appeal = appealService.queryOne(id);
        return appeal;
    }

    /**
     * 条件查询当前用户的投诉意见
     */
    @GetMapping("/getPageName")
    public R getPageByName(@RequestParam(name = "pageSize", required = false) Integer pageSize,
                           @RequestParam(name = "pageNo", required = false) Integer pageNo,
                           @RequestParam(name = "name", required = false) String name,
                           @RequestParam(name = "appealStatus", required = false) String appealStatus,
                           @RequestParam(name = "records", required = false) String records,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime,
                           @RequestParam(name = "loginName") String loginName,
                           HttpServletRequest request) {
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults<JgWgrTsjbglEntity> page = appealService.getPageByName(pageRequest, request, name, appealStatus, records,
                startTime, endTime, loginName);
        return R.ok().put("data", page);
    }

    /**
     * 反馈
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/fk")
    public R fk(String id) {
        String msg = appealService.fk(id, mainUrl);
        if (msg.equals("1")) {
            return R.error("该条信息已经反馈，请勿重复反馈！");
        } else {
            JSONObject jsonObject = (JSONObject) JSON.parse(msg);
            if (jsonObject.get("code").equals(200)) {
                return R.ok("添加消息成功");
            } else {
                return R.error("添加消息失败");
            }
        }
    }

    /**
     * 暂存投诉举报信息
     */
    @PostMapping(value = "/zcAppeal")
    public R zcAppeal(@RequestBody JgWgrTsjbglEntity jgWgrTsjbglEntity) {
        String msg = appealService.zcReply(jgWgrTsjbglEntity);
        return R.ok().put("data", msg);
    }

    /**
     * 提交投诉举报信息
     */
    @PostMapping(value = "/tjAppeal")
    public R tjAppeal(String id) {
        String s = "";
        try {
            JgWgrTsjbglEntity jgWgrTsjbglEntity = appealService.queryOne(id);
            jgWgrTsjbglEntity.setAppealStatus("1");
            appealService.update(jgWgrTsjbglEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询投诉举报个数
     */
    @GetMapping("/getAppealCount")
    public R getAppealCount() {
        String Count = appealService.getAppealCount();
        return R.ok().put("data", Count);
    }

    /**
     * 查询选中数据列表
     *
     * @param ids
     * @return
     */
    @GetMapping("/queryByIds")
    public R queryByIds(@RequestParam String ids) {
        String s = "";
        try {
            List<JgWgrTsjbglEntity> jgWgrTsjbglEntities = appealService.queryByIds(ids);
            return R.ok().put("data", jgWgrTsjbglEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询列表
     * @param name
     * @param appealStatus
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/queryByParams")
    public R queryByParams( @RequestParam(name = "records", required = false) String records,
                           @RequestParam(name = "name", required = false) String name,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime,
                            @RequestParam(name = "appealStatus", required = false) String appealStatus) {
        String s = "";
        try {
            List<JgWgrTsjbglEntity> jgWgrTsjbglEntities = appealService.queryByParams(records,name,
                    startTime, endTime, appealStatus);
            return R.ok().put("data", jgWgrTsjbglEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
   /* */ /*
    @PostMapping(value = "/addAppealTest")
    public R addAppealTest(@RequestBody List<Object> jgWgr) {
        String s = "For test";
        sysAuditService.saveSuccessAudit("新增投诉举报信息成功", Constant.AUDIT_MODULE_TSJB, Constant.PT, Constant.AUDIT_TYPE_ADD, "", request);
        return R.ok().put("data", "成功");
    }*/

}
